package com.jz.community.moduleshoppingguide.nearshop.model;

import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.baseGoods.BaseGoodsInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.moduleshoppingguide.nearshop.bean.CategoriesBean;
import com.jz.community.moduleshoppingguide.nearshop.bean.FloorGoodsInfoesBean;
import com.jz.community.moduleshoppingguide.nearshop.bean.NearShopCategoryBean;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopDownStairsBean;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopInfoBean;
import com.jz.community.moduleshoppingguide.nearshop.net.NearShopNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.List;

import io.reactivex.Observable;

public class NearShopModelImp implements NearShopModel {


    private Context mContext;

    public NearShopModelImp(Context mContext) {
        this.mContext = mContext;
    }


    @Override
    public void showShopCategorys(String filter, OnLoadListener<CategoriesBean> onLoadListener) {
        Observable<CategoriesBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NearShopNetApi.class)
                .showShopCategorys(filter));
        new RxLoadingWrapper<CategoriesBean>(mContext)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void showShopDownStairs(int page, int size, String longitude, String latitude, String message, String mainBusinessId, String limitDistance, int sort, OnLoadListener<ShopDownStairsBean> onLoadListener) {
        Observable<ShopDownStairsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NearShopNetApi.class)
                .showShopDownStairs(page, size, longitude, latitude, message, mainBusinessId, limitDistance, sort));
        new RxLoadingWrapper<ShopDownStairsBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }


    @Override
    public void floorGoodsInfoes(String shopId, String title, OnLoadListener<FloorGoodsInfoesBean> onLoadListener) {
        Observable<FloorGoodsInfoesBean> observer = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, NearShopNetApi.class)
                .floorGoodsInfoes(shopId, title));
        new RxLoadingWrapper<FloorGoodsInfoesBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void nearShopInfoes(String shopId, OnLoadListener<ShopInfoBean> onLoadListener) {
        Observable<ShopInfoBean> observer = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, NearShopNetApi.class)
                .nearShopInfoes(shopId));
        new RxLoadingWrapper<ShopInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }


    @Override
    public void deleteCartGoodsInfo(Long[] ids, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Observable<BaseResponseInfo> infoObservable = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, NearShopNetApi.class)
                .getDeleteCartGoods(ids));
        new RxLoadingWrapper<BaseResponseInfo>(mContext)
                .execute(infoObservable)
                .subscribe(baseResponseInfo -> {
                    if (baseResponseInfo == null) {
                        return;
                    }
                    onLoadListener.onSuccess(baseResponseInfo);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void getNearShopGoods(int page, int size, String filter, OnLoadListener<BaseGoodsInfo> onLoadListener) {
        Observable<BaseGoodsInfo> infoObservable = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, NearShopNetApi.class)
                .getNearShopGoods(page, size, filter));
        new RxLoadingWrapper<BaseGoodsInfo>(mContext,false)
                .execute(infoObservable)
                .subscribe(baseResponseInfo -> {
                    if (baseResponseInfo == null) {
                        return;
                    }
                    onLoadListener.onSuccess(baseResponseInfo);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void nearShopCategory(String shopId, OnLoadListener<List<NearShopCategoryBean>> onLoadListener) {
        Observable<List<NearShopCategoryBean>> infoObservable = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, NearShopNetApi.class)
                .nearShopCategory(shopId));
        new RxLoadingWrapper<List<NearShopCategoryBean>>(mContext)
                .execute(infoObservable)
                .subscribe(baseResponseInfo -> {
                    if (baseResponseInfo == null) {
                        return;
                    }
                    onLoadListener.onSuccess(baseResponseInfo);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }
}
